package com.thiswhyme.qrcode;

import java.awt.*;
import java.io.InputStream;
import java.util.List;

/**
 * @description: 二维码配置
 * @author: thiswhyme
 * @date: 2020/9/15
 */
public class QrConfig {

	/**
	 * 默认二维码配置
	 */
	public static QrConfig defaultQrCodeConfig() {
		return new QrConfig();
	}

	/**
	 * 带logo的二维码配置
	 * @param logoConfig
	 * @return
	 */
	public static QrConfig qrCodeWithLogoConfig(LogoConfig logoConfig) {
		return new QrConfig().setLogo(logoConfig);
	}

	/**
	 * 带text的二维码配置
	 * @param textConfig
	 * @return
	 */
	public static QrConfig qrCodeWithTextConfig(TextConfig textConfig) {
		return new QrConfig().setText(textConfig).setHeight(350);
	}

	/**
	 * 带logo&text的二维码配置
	 * @param logoConfig
	 * @param textConfig
	 * @return
	 */
	public static QrConfig qrCodeWithLogoTextConfig(LogoConfig logoConfig, TextConfig textConfig) {
		return new QrConfig().setText(textConfig).setLogo(logoConfig).setHeight(350);
	}

	/**
	 * 带logo&text的二维码配置
	 * @param logoConfig
	 * @param textConfig
	 * @return
	 */
	public static QrConfig qrCodeWithLogoExtraTextConfig(LogoConfig logoConfig, TextConfig textConfig, java.util.List<PositionTextConfig> extraTexts) {
		return new QrConfig().setText(textConfig).setLogo(logoConfig).setExtraTexts(extraTexts).setHeight(400);
	}

	/**
	 * 前景色（二维码颜色）
	 */
	protected int foreColor = 0xFF000000;

	/**
	 * 背景色，默认白色，null表示透明
	 */
	protected Integer backColor = 0xFFFFFFFF;

	/**
	 * 二维码宽度
	 */
	private int width = 300;

	/**
	 * 二维码高度
	 */
	private int height = 300;

	/**
	 * 边距1~4
	 */
	private int margin = 1;

	/**
	 * 二维码文件类型
	 */
	private String fileType = "JPG";

	/**
	 * logo配置
	 */
	private LogoConfig logo;

	/**
	 * 文字配置
	 */
	private TextConfig text;

	/**
	 * 附加文字配置
	 */
	private java.util.List<PositionTextConfig> extraTexts;

	/**
	 * logo配置
	 */
	public static class LogoConfig {

		public LogoConfig(InputStream image) {
			this.image = image;
		}

		public LogoConfig(InputStream image, int width, int height) {
			this.image = image;
			this.width = width;
			this.height = height;
		}

		/**
		 * logo文件
		 */
		private InputStream image;

		/**
		 * 宽度
		 */
		private int width = 60;

		/**
		 * 高度
		 */
		private int height = 60;

		public InputStream getImage() {
			return image;
		}

		public LogoConfig setImage(InputStream image) {
			this.image = image;
			return this;
		}

		public int getWidth() {
			return width;
		}

		public LogoConfig setWidth(int width) {
			this.width = width;
			return this;
		}

		public int getHeight() {
			return height;
		}

		public LogoConfig setHeight(int height) {
			this.height = height;
			return this;
		}
	}

	/**
	 * 文字配置
	 */
	public static class TextConfig {

		public TextConfig(String content) {
			this.content = content;
		}

		public TextConfig(String content, String font, int fontSize, int fontStyle, Color color) {
			this.content = content;
			this.font = font;
			this.fontSize = fontSize;
			this.fontStyle = fontStyle;
			this.color = color;
		}

		/**
		 * 文字内容
		 */
		private String content;

		/**
		 * 字体
		 */
		private String font = "宋体";

		/**
		 * 字体大小
		 */
		private int fontSize = 20;

		/**
		 * 字体风格，0：正常，1：粗体，2：斜体
		 */
		private int fontStyle = Font.PLAIN;

		/**
		 * 文字位置，默认底部
		 */
		private TextPosition position = TextPosition.BOTTOM;

		/**
		 * 字体颜色
		 */
		private Color color = Color.BLACK;

		public String getContent() {
			return content;
		}

		public TextConfig setContent(String content) {
			this.content = content;
			return this;
		}

		public String getFont() {
			return font;
		}

		public TextConfig setFont(String font) {
			this.font = font;
			return this;
		}

		public int getFontSize() {
			return fontSize;
		}

		public TextConfig setFontSize(int fontSize) {
			this.fontSize = fontSize;
			return this;
		}

		public int getFontStyle() {
			return fontStyle;
		}

		public TextConfig setFontStyle(int fontStyle) {
			this.fontStyle = fontStyle;
			return this;
		}

		public TextPosition getPosition() {
			return position;
		}

		public TextConfig setPosition(TextPosition position) {
			this.position = position;
			return this;
		}

		public Color getColor() {
			return color;
		}

		public TextConfig setColor(Color color) {
			this.color = color;
			return this;
		}
	}

	/**
	 * 自定义位置的文字配置
	 */
	public static class PositionTextConfig extends TextConfig {

		public PositionTextConfig(String content, int startX, int startY) {
			super(content);
			this.startX = startX;
			this.startY = startY;
		}

		public PositionTextConfig(String content, String font, int fontSize, int fontStyle, Color color, int startX, int startY) {
			super(content, font, fontSize, fontStyle, color);
			this.startX = startX;
			this.startY = startY;
		}

		/**
		 * 文字位置-X轴
		 */
		private int startX;

		/**
		 * 文字位置-Y轴
		 */
		private int startY;

		public int getStartX() {
			return startX;
		}

		public PositionTextConfig setStartX(int startX) {
			this.startX = startX;
			return this;
		}

		public int getStartY() {
			return startY;
		}

		public PositionTextConfig setStartY(int startY) {
			this.startY = startY;
			return this;
		}
	}

	public int getForeColor() {
		return foreColor;
	}

	public QrConfig setForeColor(int foreColor) {
		this.foreColor = foreColor;
		return this;
	}

	public Integer getBackColor() {
		return backColor;
	}

	public QrConfig setBackColor(Integer backColor) {
		this.backColor = backColor;
		return this;
	}

	public int getWidth() {
		return width;
	}

	public QrConfig setWidth(int width) {
		this.width = width;
		return this;
	}

	public int getHeight() {
		return height;
	}

	public QrConfig setHeight(int height) {
		this.height = height;
		return this;
	}

	public int getMargin() {
		return margin;
	}

	public QrConfig setMargin(int margin) {
		this.margin = margin;
		return this;
	}

	public String getFileType() {
		return fileType;
	}

	public QrConfig setFileType(String fileType) {
		this.fileType = fileType;
		return this;
	}

	public LogoConfig getLogo() {
		return logo;
	}

	public QrConfig setLogo(LogoConfig logo) {
		this.logo = logo;
		return this;
	}

	public TextConfig getText() {
		return text;
	}

	public QrConfig setText(TextConfig text) {
		this.text = text;
		return this;
	}

	public java.util.List<PositionTextConfig> getExtraTexts() {
		return extraTexts;
	}

	public QrConfig setExtraTexts(List<PositionTextConfig> extraTexts) {
		this.extraTexts = extraTexts;
		return this;
	}
}
